package cn.lg.soar.database.base.entity;

import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

/**
 * 树形数据，使用左右值实现
 * @author luguoxiang
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@ApiModel(value = "树形数据", description = "树形数据基础")
@EqualsAndHashCode(callSuper=false)
@Getter
@Setter
public abstract class NumberTreeEntity<ID extends Serializable> extends TreeEntity<ID> implements INumberTreeEntity<ID> {

    private static final long serialVersionUID = 2562184700379561956L;

    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    @ApiModelProperty(value = "左值")
    protected Integer leftNo;

    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    @ApiModelProperty(value = "右值")
    protected Integer rightNo;

    @Override
    @ApiModelProperty(value = "后代数量（为0是叶子节点）")
    public Integer getDescendantQuantity() {
        if (leftNo == null || rightNo == null) {
            return null;
        }
        return (rightNo - leftNo - 1) >> 1;
    }

}
